package kr.co.tomatosystem.pde.tools.internal.nature;


import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.handlers.HandlerUtil;

public class UninstallNatureHandler extends AbstractHandler implements IHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
		if (!(currentSelection instanceof IStructuredSelection)) {
			return null;
		}

		Object firstSelection = ((IStructuredSelection) currentSelection).getFirstElement();
		IProject project = (IProject) Platform.getAdapterManager().getAdapter(firstSelection, IProject.class);

		if (project == null) {
			return null;
		}

		try {
			ResourceAccessorNature.uninstallNature(project, new NullProgressMonitor());
		} catch (CoreException e) {
			e.printStackTrace();
		}

		return null;
	}

}
